<?php
namespace app\common\validate\appeal;
use app\common\model\appeal\ShopOrderAppealModel;
use think\Validate;
/**
 * 申诉中心验证器
 */
class ShopOrderAppealValidate extends Validate
{
    // 验证规则
    protected $rule = [
        'ids' => ['require', 'array'],
        'id' => 'require',
        'is_disable' => ['require', 'in' => '0,1'],
        'shop_order_id' => ['require', 'checkAppealExisted'],
        'setting_appeal_type_id' => 'require',
        'content'=>'require',
        'order_id'=>'require',
    ];
    // 错误信息
    protected $message = [
        'shop_order_id.require' => '请输入订单编号',
        'setting_appeal_type_id.require' => '请选择申诉类型',
        'content.require' => '请输入申诉内容',
        'order_id.require' => '请选择申诉订单',
    ];
    // 验证场景
    protected $scene = [
        'info'    =>  ['id'],
        'add'     =>  ['shop_order_id','setting_appeal_type_id'],//申诉
        'edit'    =>  ['id', ],
        'dele'    =>  ['ids'],
        'disable' =>  ['ids', 'is_disable'],
        'reply'    =>  ['id','content'],
        'end'    =>  ['ids'],
        'infoByOrderID'=>  ['order_id'],
    ];
    /**
     * 订单申诉验证该
     * @Author: 易军辉
     * @DateTime:2024-06-28 10:58
     * @param $value
     * @param $rule
     * @param $data
     * @return string|true
     */
    protected function checkAppealExisted($value, $rule, $data = [])
    {
        $model = new ShopOrderAppealModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['shop_order_id', '=', $data['shop_order_id']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->whereNotIn('status',[2,3])->find();
        if ($info) {
            return '该订单已存在未处理完成的申诉';
        }
        return true;
    }
}
